package com.wh.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wh.exception.SytException;
import com.wh.exception.SytExceptionEnum;
import com.wh.mapper.PatientMapper;
import com.wh.model.pojo.Patient;
import com.wh.model.vo.PatientVo;
import com.wh.service.PatientService;
import com.wh.util.RandomNum;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: wh(1835734390 @ qq.com)
 * @date: 2022/11/19 8:58
 * @description:
 * @version:
 */
@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient> implements PatientService {
    @Autowired
    private PatientMapper patientMapper;
    @Override
    public String insertInfo(PatientVo patientVo) {
        Patient patient = new Patient();
        BeanUtils.copyProperties(patientVo,patient);
        patient.setId(RandomNum.randomId());
        int insert = patientMapper.insert(patient);
        if (insert < 0){
            throw new SytException(SytExceptionEnum.INSERT_FAILED);
        }
        return patient.getId();
    }

    @Override
    public List<Patient> getPatientList() {
        List<Patient> patientList = new ArrayList<>();
        patientList = patientMapper.selectList(null);
        return patientList.stream().sorted(Comparator.comparing(Patient::getDefaultFlag).reversed()).collect(Collectors.toList());
    }
}
